package com.flca.frw.components
{
	import com.flca.frw.util.ValidationUtil;
	import com.flca.frw.config.TranslateUtilFactory;
	import com.flca.frw.util.ValidationUtil;
	import mx.containers.VBox;
	import flash.display.DisplayObject;
	import mx.controls.TextInput;
	import com.flca.frw.interfaces.ICrudCtlr;
	import com.flca.frw.interfaces.ISimpleCtlr;
	import com.flca.frw.util.ConversionUtil;
	import mx.core.UIComponent;
	import com.flca.frw.util.EnumHelper;

	public class SimpleBaseCtrl extends VBox implements ISimpleCtlr
	{
		public const UPD_VIEW:Boolean = true;
		public const UPD_MODEL:Boolean = false;

		[Inspectable]
		[Bindable]
		public var labelWidth:int;

		protected var modelGetter : Function;

		public function setModelGetter(value:Function) : void {
			modelGetter = value;			
		}
	
		public function init(aInitialHash:int = 0) : void {
			//overide this method if you want something to happen
			//when this control comes up
			updateViewFromModel();
		}

		
		public function SimpleBaseCtrl() {
			super();
		}
		
		public function updateViewFromModel():void
		{
			// OVERRIDE this method
			
			// This method is typically used to copy the
			// model values to the UI controls
			
			return;
		}

		public function updateModelFromView():void
		{
			// OVERRIDE this method
			
			// This method is typically used to copy the
			// model values to the UI controls
			
			return;
		}

		public function validate() : Boolean {
			return validateView() == null;
		}
		
		public function validateView():String
		{
			// OVERRIDE this method only if additional validation
			// is required. Don't forget to call this method from
			// the overriden method!!!
			// If all validations are passed 'null' should be returned,
			// otherwise a compelling message about the validation error
			
			var result:String;
			
			ValidationUtil.enableValidators(getValidators(), true);
			if (ValidationUtil.validateAll(getValidators()))
			{
				result = null;
			}
			else
			{
				result = "Not all validations are passed.";
			}
			
			return result;
			
		}
			
		public function getValidators():Array
		{
			// OVERRIDE this method
			
			// Normally this method returns all
			// relevant validators of the view
			
			return new Array();
		}
		
		public function trl(value: String) : String
		{
			return value;
			//return TranslateUtilFactory.getInstance().translate(value);
		}

		public function updateField(fld:UIComponent, value:Object, setter:Function, m2v:Boolean): void {
			ConversionUtil.updateField(fld, value, setter, m2v);
		}
		
		public function updateComboBox(fld:UIComponent, value:String, setter:Function, enumValues:Array, m2v:Boolean, labelFunc:Function=null): void {
			if (m2v) {
				EnumHelper.updateToCombobox(fld, value, enumValues, labelFunc);			
			} else {
				EnumHelper.updateFromCombobox(fld, setter, enumValues);							
			}
		}
		
		public function updateStringWithComboBox(fld:UIComponent, value: String, setter:Function, values:Array, m2v:Boolean, labelFunc:Function=null): void {
			if (m2v) {
				EnumHelper.updateStringToCombobox(fld, value, values, labelFunc);			
			} else {
				EnumHelper.updateStringFromCombobox(fld, setter, values);							
			}
		}
		
		public function close() : void {
			//override if needed to do some cleanup
		}
	}
}